package org.jeecgframework.web.service.impl;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.service.ZyFileLogServiceI;
import org.jeecgframework.web.system.pojo.base.ZyFileLogEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.io.File;
import java.io.Serializable;

@Service("zyFileLogService")
@Transactional
public class ZyFileLogServiceImpl extends CommonServiceImpl implements ZyFileLogServiceI {


    public <T> void delete(T entity) {
        super.delete(entity);
        //执行删除操作配置的sql增强
        this.doDelSql((ZyFileLogEntity) entity);
    }

    public <T> Serializable save(T entity) {
        Serializable t = super.save(entity);
        //执行新增操作配置的sql增强
        this.doAddSql((ZyFileLogEntity) entity);
        return t;
    }

    public <T> void saveOrUpdate(T entity) {
        super.saveOrUpdate(entity);
        //执行更新操作配置的sql增强
        this.doUpdateSql((ZyFileLogEntity) entity);
    }

    /**
     * 默认按钮-sql增强-新增操作
     *
     * @param id
     * @return
     */
    public boolean doAddSql(ZyFileLogEntity t) {
        return true;
    }

    /**
     * 默认按钮-sql增强-更新操作
     *
     * @param id
     * @return
     */
    public boolean doUpdateSql(ZyFileLogEntity t) {
        return true;
    }

    /**
     * 默认按钮-sql增强-删除操作
     *
     * @param id
     * @return
     */
    public boolean doDelSql(ZyFileLogEntity t) {
        return true;
    }

    /**
     * 替换sql中的变量
     *
     * @param sql
     * @return
     */
    public String replaceVal(String sql, ZyFileLogEntity t) {
        sql = sql.replace("#{id}", String.valueOf(t.getId()));
        sql = sql.replace("#{business_type}", String.valueOf(t.getBusinessType()));
        sql = sql.replace("#{file_name}", String.valueOf(t.getOldFileName()));
        sql = sql.replace("#{file_path}", String.valueOf(t.getFilePath()));
        sql = sql.replace("#{file_type}", String.valueOf(t.getFileType()));
        sql = sql.replace("#{create_name}", String.valueOf(t.getCreateName()));
        sql = sql.replace("#{create_by}", String.valueOf(t.getCreateBy()));
        sql = sql.replace("#{create_date}", String.valueOf(t.getCreateDate()));
        sql = sql.replace("#{update_name}", String.valueOf(t.getUpdateName()));
        sql = sql.replace("#{update_by}", String.valueOf(t.getUpdateBy()));
        sql = sql.replace("#{update_date}", String.valueOf(t.getUpdateDate()));
        sql = sql.replace("#{UUID}", UUID.randomUUID().toString());
        return sql;
    }

    public void deleteFileLogAndFile(String zyFileLogId) {
        if (StringUtil.isNotEmpty(zyFileLogId)) {
            ZyFileLogEntity zyFileLog = super.get(ZyFileLogEntity.class, zyFileLogId);
            if (null != zyFileLog) {
                String filePath = zyFileLog.getFilePath();
                if (StringUtil.isNotEmpty(filePath)) {
                    File file = new File(filePath);
                    if (file.exists()) {
                        file.delete();
                    }
                }
                super.delete(zyFileLog);
            }
        }
    }

    @Override
    public List<Map<String, Object>> getcompleteFile(String fileBeanId, String type, String zptype) {

        String sql = "SELECT\n" +
                "	*\n" +
                "FROM\n" +
                "	`zy_file_log`\n" +
                "WHERE\n" +
                "	business_key = '" + fileBeanId + "'\n";

        if(StringUtil.isNotEmpty(type)){
            if (type.equals("wx")) {
                sql += "and business_type LIKE '%XC%'";
            }
            if (type.equals("xc")) {
                if (zptype.equals("yqfx")) {
                    sql += "and business_type LIKE '%G%'";
                }
                if (zptype.equals("qt")) {
                    sql += "and business_type LIKE '%F%'";
                }

            }
            if (type.equals("yq")) {
                sql += "and business_type LIKE '%H%'";
            }
            if (type.equals("gg")) {
                sql += "and (business_type LIKE '%IF%' or business_type LIKE '%IP%')";
            }
        }

        return this.findForJdbc(sql);
    }
}